
#ifndef __PT_H__
#define __PT_H__

#include "lc.h"

struct pt {
  lc_t lc;
};

#define PT_THREAD_WAITING 0
#define PT_THREAD_EXITED  1


#define PT_THREAD(name_args) char name_args


#define PT_INIT(pt)						\
  LC_INIT((pt)->lc)


#define PT_BEGIN(pt) LC_RESUME((pt)->lc)



#define PT_WAIT_UNTIL(pt, condition)    \
  do {									\
    LC_SET((pt)->lc);					\
    if(!(condition)) {					\
      return PT_THREAD_WAITING;			\
    }									\
  } while(0)


#define PT_WAIT_WHILE(pt, cond)			\
  PT_WAIT_UNTIL((pt), !(cond))



#define PT_WAIT_THREAD(pt, thread)		\
  PT_WAIT_UNTIL((pt), (thread))


#define PT_SPAWN(pt, thread)			\
  do {									\
    PT_INIT((pt));						\
    PT_WAIT_THREAD((pt), (thread));		\
  } while(0)


#define PT_RESTART(pt)					\
  do {									\
    PT_INIT(pt);						\
    return PT_THREAD_WAITING;			\
  } while(0)


#define PT_EXIT(pt)						\
  do {									\
    PT_INIT(pt);						\
    return PT_THREAD_EXITED;			\
  } while(0)


#define PT_END(pt) LC_END((pt)->lc); PT_EXIT(pt)



#define PT_SCHEDULE(f) (f == PT_THREAD_WAITING)

#endif /* __PT_H__ */
